package itsm.isperp.module.controller.project;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjCostAccountingEntry;
import itsm.isperp.module.service.project.ProjCostAccountingEntryService;
import itsm.isperp.module.service.project.ProjCostTemplateService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 会计分录管理控制器
 * 
 * @author lizx
 * @date 2013-10-2
 * @version V1.0
 */
@Controller
@RequestMapping("project/costAccountingEntry")
public class ProjCostAccountingEntryController extends
		BaseController<ProjCostAccountingEntry> {

	@Autowired
	protected ProjCostAccountingEntryService projCostAccountingEntryService;

	@Autowired
	ProjCostTemplateService projCostTemplateService;

	@Override
	public ProjCostAccountingEntryService getService() {
		return this.projCostAccountingEntryService;
	}

	@Override
	public String getPageTitle() {
		return "会计分录管理";
	}

	@RequestMapping("listByRelationId")
	@ResponseBody
	public PageResponse<Map<String, Object>> listByRelationId(
			JqGridRequest request, HttpServletRequest req) {

		String relationId = req.getParameter("relationId");

		if (StringUtils.isNotEmpty(relationId)) {
			request.addFilterRule("relationId", JqGridFilterRuleOp.eq,
					relationId);
		}
		return this.getService().findAllDetailByRequest(request).toPage();
	}
	
	@RequestMapping("saveCostAccount")
	@ResponseBody
	public ResultMessage saveCostAccount(HttpServletRequest req) {

		String tdata=req.getParameter("tdata");
		String projectId=req.getParameter("projectId");
		String sql1="";
		String curUserName=ContextHolder.getLoginUsername();
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		Map<String,Object> mp=JsonBuilder.buildNormalBinder().fromJson(tdata, Map.class);
		if(mp.size()>0)
		{
			for (Map.Entry<String,Object> entry : mp.entrySet()) {  
				sql1="select id from proj_cost_template where code='"+entry.getKey()+"' limit 1";
				String tid=jdbcTemplate.queryForObject(sql1,String.class);
				JqGridRequest request=new JqGridRequest();
				request.setSearchString(" cost_template_id='"+tid+"' and project_id='"+projectId+"'");
				ProjCostAccountingEntry pae=this.getService().findOneByRequest(request);
				if(pae==null)
				{
					ProjCostAccountingEntry newPae=new ProjCostAccountingEntry();
					newPae.setCostTemplateId(tid);
					newPae.setProjectId(projectId);
					newPae.setAmount(Float.parseFloat(entry.getValue().toString()));
					newPae.setHandlerId(curUserName);
					this.getService().saveOrUpdate(newPae);
				}else
				{
					pae.setAmount(Float.parseFloat(entry.getValue().toString()));
					pae.setHandlerId(curUserName);
					this.getService().saveOrUpdate(pae);
				}
			} 
		}
		
		return ResultMessage.newInstance(true);
	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		String relationId = req.getParameter("relationId");

		String projectId = req.getParameter("projectId");
		if (StringUtils.isEmpty(projectId)) {
			return null;
		}

		ProjCostAccountingEntry model = null;
		if (StringUtils.isEmpty(id)) {
			model = new ProjCostAccountingEntry();
			model.setRelationId(relationId);
			model.setProjectId(projectId);
			IUser user = ContextHolder.getLoginUser();
			model.setCreator(user.getUsername());

			model.setHandlerId(user.getUsername());
			model.setHandlerName(user.getFullname());
			model.setOccurrenceDate(new Date());
		} else {
			model = this.getService().findOne(id);
		}

		List<Map<String, Object>> list = projCostTemplateService
				.findAcccountingItemByProject(projectId);
		mv.addObject("projCostTemplates", list);

		mv.addObject("model", model);

		return mv;
	}
}
